//
// google_reader.cpp
//
// Library: SocialBox
// Package:
// Module:  google_reader
//
// Copyright (c) 2007, Bertrand Cachet and Contributors.
//

#include "google_reader.hpp"

/****************************************************
*       POCO dependencies
****************************************************/
#include <Poco/RegularExpression.h>
#include <Poco/URI.h>
#include <Poco/DateTime.h>
#include <Poco/StreamCopier.h>

/****************************************************
*       BOOST dependencies
****************************************************/
#include <boost/algorithm/string/regex.hpp>
#include <boost/regex.hpp>

/****************************************************
*       System dependencies
****************************************************/
#include <list>
#include <iostream>
#include <sstream>
#include <utility>
#include <string>
#include <vector>

/****************************************************
*       SocialBox dependencies
****************************************************/
#include "web_client.hpp"

/****************************************************
*       Namespaces
****************************************************/
using namespace Poco;
using namespace std;

string GoogleReader::SOURCE = "socialbox";
string GoogleReader::ROOT_URL = "www.google.com";
string GoogleReader::LOGIN_URL = "http://" + ROOT_URL + "/accounts/ClientLogin";


Result::Result() {
    status = 0;
}

Result::~Result() {
}

GoogleReader::GoogleReader(string username, string password,
                            WebClient::SynchronizationMode mode) {
    _username = username;
    _password = password;
    _webClient = new WebClient(mode);
    _buffers = new HashMap<string, ostream*>();
    _results = new HashMap<string, Result*>();
}

GoogleReader::GoogleReader(WebClient::SynchronizationMode mode) {
    _username = "";
    _password = "";
    _webClient = new WebClient(mode);
    _buffers = new HashMap<string, ostream*>();
    _results = new HashMap<string, Result*>();
}

GoogleReader::~GoogleReader() {
    delete _webClient;
    delete _buffers;
    delete _results;
}

string GoogleReader::getUsername() {
    return _username;
}

string GoogleReader::getPassword() {
    return _password;
}
void GoogleReader::setUsername(string username) {
    _username = username;
}
void GoogleReader::setPassword(string password) {
    _password = password;
}

int GoogleReader::_parseLogin(istream *data) {
    vector<string> results;
    string SID;
    string result;
    /*
    QNetworkCookie cookie;
    QNetworkCookieJar *cookiejar = _webClient->cookieJar();
    */
    URI cookieUrl("http://" + ROOT_URL);
    // list<QNetworkCookie> cookies = cookiejar->cookiesForUrl(cookieUrl);
    DateTime expirationDate = DateTime();
    
    //RegularExpression re("SID=(\\w*)");

    StreamCopier::copyToString(*data, result);
    boost::algorithm::split_regex(results, result,
                                  boost::regex("SID=(\\w*)"));
    //re.split(result, results);
    /*
    // We remove all the previously stored cookies.
    foreach(QNetworkCookie c, cookies){
        if (c.name() == "SID")
            cookies.removeOne(c);
    }
    */
    if (results.size() > 0) {
        // SID = results[0].replace("SID=", "");
        /*
        cookie = QNetworkCookie("SID", SID.toUtf8());
        expirationDate = expirationDate.addMonths(6);
        cookie.setExpirationDate(expirationDate);
        cookie.setPath("/");
        cookie.setDomain(".google.com");
        cookies.append(cookie);
        cookiejar->setCookiesFromUrl(cookies, cookieUrl);
        */
    }

//    foreach (QNetworkCookie c,
//                _webClient->cookieJar()->cookiesForUrl(cookieUrl))
//        qDebug() << "Cookie:" << c.toRawForm();

    return SID.empty() ? -1 : 0;
}

int GoogleReader::login(string username, string password) {
    _username = username;
    _password = password;
    return login();
}

int GoogleReader::login() {
    HashMap<string, string> args = HashMap<string, string>();
    int status = 0;
    ostream* buffer = new stringstream(stringstream::in | stringstream::out);
    if (!_username.empty()) {
        cout << "GoogleReader::Login: "<< LOGIN_URL << buffer << _buffers << endl;
        _buffers->insert(pair<string, ostream*>(LOGIN_URL, buffer));
        cout << "GoogleReader::Login: Make association between buffer and URL" << endl;
        args["Email"] = _username;
        args["Passwd"] = _password;
        args["Service"] = "reader";
        args["Source"] = SOURCE;
        args["Continue"] = ROOT_URL;
        cout << "GoogleReader::Login: Setup arguments" << endl;
        status = _webClient->post(LOGIN_URL, args, buffer);
        if (_webClient->getSynchronizationMode() == WebClient::Synchronous) {
                _results->erase(LOGIN_URL);
        }
    } else {
        status = -1;
    }
    return status;
}
